/**
 * Created by Administrator on 2017/1/10.
 */

Array.prototype.sum = function() {
    var total = 0;
    for(var i=0; i<this.length; i++) {
        if( !isNaN(this[i]) ) {
            total += +this[i];
        }
    }

    return total;
}

Array.prototype.remove = function(item) {
    var index = this.indexOf(item);

    if( index >= 0){
        this.splice(index, 1);
    }
}

Array.prototype.merge = function(arr) {
    for (var i = 0; i < arr.length; ++i) {
        this.push(arr[i]);
    }
}

String.prototype.isStartWith = function(prefix) {
    if( !prefix || !this.length || this[0] != prefix[0] ) return false;
    return (this.substr(0, prefix.length) == prefix);
}

String.prototype.isEndWith = function(suffix) {
    if( !suffix || !this.length || suffix.length > this.length) return false;
    return (this.substr(this.length - suffix.length) == suffix);
}

String.prototype.format = function() {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g, function(m,i){
        return args[i];
    });
}

String.prototype.capWord = function() {
    return this.substr(0,1).toUpperCase() + this.substr(1);
}

// 格式化时间
Date.prototype.format = function(fmt)   {
    var o = {
        'M+' : this.getMonth()+1,                 //月份
        'd+' : this.getDate(),                    //日
        'h+' : this.getHours(),                   //小时
        'm+' : this.getMinutes(),                 //分
        's+' : this.getSeconds(),                 //秒
        'q+' : Math.floor((this.getMonth()+3)/3), //季度
        'S'  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)) {
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+'').substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp('('+ k +')').test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ?
                (o[k]) : (('00'+ o[k]).substr((''+ o[k]).length)));
        }
    }
    return fmt;
}

// 计算UTC毫秒
Date.prototype.getUTCFullMilliseconds = function() {
    return this.getTime() - this.getTimezoneOffset()*60000;
}

// 计算UTC天数
Date.prototype.getDays = function() {
    return Math.floor(this.getUTCFullMilliseconds()/86400000);
}

// 计算UTC周数
Date.prototype.getWeeks = function() {
    return Math.ceil((4 + this.getDays()) / 7);
}

// 获取距离refTime经过的天数, refTime(时间戳|2013-01-01|2013-01-01 00:00:00)
Date.prototype.getDayPassed = function(refTime) {
    if( isNaN(refTime) ) {
        if( refTime.length > 10 ) {
            refTime = refTime.substr(0, 10);
        }
        refTime = new Date(refTime + ' 00:00:00');
    }else{
        refTime = new Date(refTime * 1000);
    }

    refTime.setHours(0);
    refTime.setMinutes(0);
    refTime.setSeconds(0);

    return Math.ceil((this - refTime)/1000/86400);
}

// 计算UTC时间戳
Date.prototype.getUTCStamp = function() {
    return Math.floor(this.getUTCFullMilliseconds()/1000);
}

// 计算本地时间戳
Date.prototype.getStamp = function() {
    return Math.floor(this.getTime()/1000);
}

// 设置当天的时间,支持小数,比如setFloatTime(10.5, 0, 0)即为设置为10点半
Date.prototype.setFloatTime = function(hour, minute, second) {
    var totalMinutes = Math.ceil(hour*3600 + minute*60 + second);
    hour = Math.floor(totalMinutes/3600);
    totalMinutes = totalMinutes%3600;
    minute = Math.floor(totalMinutes/60);
    second = totalMinutes%60;

    this.setHours(hour);
    this.setMinutes(minute);
    this.setSeconds(second);
    return this;
}

// 当天0点
Date.prototype.zeroTime = function() {
    return new Date(this.getFullYear(), this.getMonth(), this.getDate());
}

/**
 * 周一0点
 */
Date.prototype.zeroWeek = function() {
    var weekDay = this.getDay();
    if (weekDay == 0) {
        weekDay = 7;
    }

    return this.zeroTime().relativeTime((1 - weekDay) * 24*60*60);
}

/**
 * 月初零点
 */
Date.prototype.zeroMonth = function() {
    return new Date(this.getFullYear(), this.getMonth(), 1);
}

// 相对时间(秒)
Date.prototype.relativeTime = function(seconds) {
    return new Date(this.getTime() + seconds*1000);
}

// 标准格式化字符串
Date.prototype.stdFormatedString = function() {
    return this.format('yyyy-MM-dd hh:mm:ss');
}

// 以数字形式表示的年月日
Date.prototype.getDateNumber = function() {
    return this.getFullYear()*10000 + (this.getMonth()+1)*100 + this.getDate();
}

// 以数字形式表示的年月
Date.prototype.getMonthNumber = function() {
    return this.getFullYear()*100 + (this.getMonth()+1);
}

Date.prototype.getTodaySeconds = function() {
    return this.getHours()*3600 + this.getMinutes()*60 + this.getSeconds();
}

Date.getDayPassed = function(refTime) {
    return (new Date()).getDayPassed(refTime);
}

Date.getStamp = function() {
    return (new Date()).getStamp();
}

Date.createFromStamp = function(stamp) {
    return new Date(stamp*1000);
}

Date.stdFormatedString = function() {
    return (new Date()).stdFormatedString();
}

/**
 * 根据UTC天数计算UTC周数
 * @param day
 * @returns {number}
 */
Date.calcWeeksFromDays = function(day) {
    return Math.ceil((4 + day) / 7);
}